package com.coollang.smartbra.activity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.beans.SoftBean;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.PackageUtils;
import com.coollang.smartbra.utils.StringUtils;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.views.BaseLineView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Created by han on 2016/10/31.
 *
 * @author han
 * @date 2016/10/31
 * @Modifier ${MODIFIER}
 * @ModifiedTime ${MODIFIEDDATE}
 * @ModifyContent${MODIFYCONTENT}
 * @since ${VERSIONNBAME}
 */
public class SystemSettingActivity extends MyAutoLayoutActivity implements RippleView.OnRippleCompleteListener {
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.blv_userinfo)
    BaseLineView blvUserinfo;
    @BindView(R.id.blv_clear)
    BaseLineView blvClear;
    @BindView(R.id.blv_grade)
    BaseLineView blvGrade;
    @BindView(R.id.blv_share_app)
    BaseLineView blvShareApp;
    @BindView(R.id.blv_update_app)
    BaseLineView blvUpdateApp;
    @BindView(R.id.blv_about_app)
    BaseLineView blvAboutApp;
    @BindView(R.id.exit_app)
    Button exitApp;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.rv_back)
    RippleView rvBack;

    private String versionCode = "1.0.0";
    private String serverCode = "1.0.0";

    @Override
    protected void initView() {
        setContentView(R.layout.activity_system_setting);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
    }

    private void initViews() {
        rvBack.setOnRippleCompleteListener(this);
        tvTitle.setText(getString(R.string.system_setting));
        tvTitle.setTextColor(getResources().getColor(R.color.run_setcolor));
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void onEventMainThread(CommonEvent event) {
        if (event.type == CommonEvent.UPDATESOFTWARE) {
            switch (event.what) {
                case CommonEvent.REQUESTSUEECCD:
                    SoftBean soft = new SoftBean();
                    soft = (SoftBean) StringUtils.parseJSonSafe(event.msg, soft);
                    if (soft != null) {
                        serverCode = soft.getData().getVersion();
                        // 获取版本号,判断是否升级
                        if (hasNewVersion(soft.getData().getVersion())) {
//                            showUpdateTip(true, soft);
                            ToastUtils.showToast("要更新");
                        } else {
                            ToastUtils.showToast("已经是最新");
//                            showUpdateTip(false, null);
                        }
                    } else {
                        ToastUtils.showToast(getString(R.string.network_erro));
                    }
                    break;
                case CommonEvent.REQUESTFAILUE:
                    ToastUtils.showToast(event.msg);
                    break;
                default:
                    break;
            }
        }
    }

    private boolean hasNewVersion(String version) {
        // 获取软件版本
        versionCode = PackageUtils.getAppVersionName();
        return PackageUtils.compareVersion(versionCode, version);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.blv_userinfo, R.id.blv_clear, R.id.blv_grade, R.id.blv_share_app, R.id.blv_update_app, R.id.blv_about_app, R.id.exit_app})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.blv_userinfo:
                ActivitySwitcher.goToSelfInfoEdit(this);
                break;
            case R.id.blv_clear:
                break;
            case R.id.blv_grade:
                break;
            case R.id.blv_share_app:
                break;
            case R.id.blv_update_app:
                // HttpUtil.updateSoftware();
                break;
            case R.id.blv_about_app:
                break;
            case R.id.exit_app:
                MyApplication.getApplication().needLoginAgain();
                break;
        }
    }

    @Override
    public void onComplete(RippleView rippleView) {
        finish();
    }
}
